const APP = getApp()
var util = require('./util.js')
var request = new Object()

// 设置token
request.setToken = function (tokenName, data) {
    console.log('setToken')
    wx.setStorage({
        key: tokenName,
        data: data
    })
}
//获取token
request.getToken = function (tokenName) {
    return wx.getStorageSync(tokenName)
}
//移除token
request.removeToken = function (tokenName) {
    wx.removeStorageSync(tokenName)
}

// 前端登录接口   //不需要token
request.loginRequest = function (obj) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: util.config().api + obj.url,
            data: obj.data,
            method: obj.method,
            header: {
                "Content-Type": "application/json;charset=utf-8"
            },
            success: function (res) {
                if (res.data.code === 200) {
                    resolve(res)
                    request.setToken('jy-token', res.data.data)
                    wx.showToast({
                        title: res.data.msg,
                        icon: "success"
                    })
                } else {
                    console.log('err', res)
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                    reject(res)
                }
            },
            fail: function (err) {
                reject(err)
                wx.showToast({
                    title: err.data.msg,
                    icon: none
                })
            }
        })
    })
}
// 前端请求接口    //需要token
request.wxRequest = function (obj) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: util.config().api + obj.url,
            data: obj.data,
            method: obj.method,
            header: {
                "Content-Type": "application/json;charset=utf-8",
                'Authorization': 'Bearer ' + request.getToken('jy-token')
            },
            success: function (res) {

                if (res.data.code === 401) {
                    // wx.showToast({
                    //   title: '登录失效，请重新登录哦！',
                    //   icon: "none"
                    // })
                    resolve(res)
                    if (!!request.getToken('jy-token')) {
                        request.removeToken('jy-token')
                        wx.reLaunch({
                            url: '/pages/loginIndex/loginIndex',
                        })
                    }
                } else if (res.data.code === 200) {
                    resolve(res)
                } else if (res.data.msg == '' && res.data.code != 200) {
                    wx.showToast({
                        title: '请先登录哦！',
                        icon: "none"
                    })
                    request.removeToken('jy-token')
                } else if (!!res.data.msg && res.data.code != 200) {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                }
            },
            fail: function (err) {
                reject(err)
                wx.showToast({
                    title: res.data.code + '，' + err.data.msg,
                    icon: "none"
                })
            }
        })
    })


}
// 前端请求接口     //不需要token
request.passRequest = function (obj) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: util.config().api + obj.url,
            data: obj.data,
            method: obj.method,
            header: {
                "Content-Type": "application/json;charset=utf-8",
            },
            success: function (res) {

                if (res.data.code === 200) {
                    resolve(res)
                } else {
                    console.log('err', res)
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                    reject(res)
                }
            },
            fail: function (err) {
                wx.showToast({
                    title: res.data.code + '，' + err.data.msg,
                    icon: "none"
                })
                reject(err)
            }
        })
    })


}

//后端登录接口    //不需要token
request.backLoginRequest = function (obj) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: util.config().api + obj.url,
            data: obj.data,
            method: obj.method,
            header: {
                "Content-Type": "application/json;charset=utf-8"
            },
            success: function (res) {
                if (res.data.code === 200) {
                    resolve(res)
                    request.setToken('jy-bToken', res.data.token)
                    wx.showToast({
                        title: res.data.msg,
                        icon: "success"
                    })
                } else {
                    console.log('err', res)
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                }
            },
            fail: function (err) {
                reject(err)
                wx.showToast({
                    title: err.data.msg,
                    icon: none
                })
            }
        })
    })
}

// 后端请求接口
request.backWxRequest = function (obj) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: util.config().api + obj.url,
            data: obj.data,
            method: obj.method,
            header: {
                "Content-Type": "application/json;charset=utf-8",
                'Authorization': 'Bearer ' + request.getToken('jy-bToken')
            },
            success: function (res) {
                if (res.data.code === 401) {
                    wx.showToast({
                        title: '登录失效，请重新登录哦！',
                        icon: "none"
                    })
                    resolve(res)
                    if (!!request.getToken('jy-bToken')) {
                        request.removeToken('jy-bToken')
                        wx.reLaunch({
                            url: '/pages/login/login?whoLogin=' + false,
                        })
                    } else {
                        wx.reLaunch({
                            url: '/pages/login/login?whoLogin=' + false,
                        })
                    }

                } else if (res.data.code === 200) {
                    resolve(res)
                } else if (res.data.msg == '' && res.data.code != 200) {
                    wx.showToast({
                        title: '请先登录哦！',
                        icon: "none"
                    })
                    request.removeToken('jy-bToken')
                } else if (!!res.data.msg && res.data.code != 200) {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                }
            },
            fail: function (err) {
                reject(err)
                wx.showToast({
                    title: err.data.msg,
                    icon: "none"
                })
            }
        })
    })


}


module.exports = request
